package com.hr.checkrepetition.enums;

import com.hr.checkrepetition.enums.serializer.ResponseStatusSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * 响应状态对象
 *
 * @author leo wu
 * @date 2018-03-15
 */
@JsonSerialize(using = ResponseStatusSerializer.class)
public enum ResponseStatusEnum {

    OK("200", "成功"),
    PARAMETER_ERROR("301", "参数错误"),

    ;


    private String code;
    private String message;

    public static ResponseStatusEnum getEnum(String code) {
        for (ResponseStatusEnum ele : ResponseStatusEnum.values()) {
            if (ele.code.equals(code)) {
                return ele;
            }
        }
        return null;
    }

    /**
     * 构造方法
     *
     * @param code    错误码
     * @param message 错误消息
     */
    ResponseStatusEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, Object> toMap() {
        return ImmutableMap.<String, Object>builder()
                .put("message", message)
                .put("code", code)
                .build();
    }
}
